package com.wanli.storemobile.homepage.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wanli.storemobile.R;
import com.wanli.storemobile.bean.PurchaseListBean;
import com.wanli.storemobile.utils.DateTimeUtil;
import com.wanli.storemobile.utils.NumberFormatUtils;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 消息列表
 */
public class PurchaseHomeListAdapter extends RecyclerView.Adapter<PurchaseHomeListAdapter.ViewHolder> {

    private OnCallBackListener onCallBackListener;
    private List<PurchaseListBean.DataBean.ItemListBean> listsBeans;
    private Context context;

    public PurchaseHomeListAdapter(List<PurchaseListBean.DataBean.ItemListBean> listsBeans) {
        this.listsBeans = listsBeans;
    }

    public void setOnCallBackListener(OnCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_purchase_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        PurchaseListBean.DataBean.ItemListBean dataBean = listsBeans.get(position);

        holder.tvOrderNo.setText(dataBean.getOrder_no());
        holder.tvName.setText(dataBean.getPurchaser());
        String orderFee = NumberFormatUtils.formatDouble(Double.parseDouble(dataBean.getOrder_fee()) / 100);
        holder.tvTotalMoney.setText(orderFee+"元");
        holder.tvTime.setText(DateTimeUtil.format7(new Date(Long.parseLong(dataBean.getCreate_time()) * 1000)));
        holder.tvInformation.setOnClickListener(v -> {
            onCallBackListener.onCallBack(1, position);
        });
        holder.tvDelete.setOnClickListener(v -> {
            onCallBackListener.onCallBack(2, position);
        });
    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_total_money)
        TextView tvTotalMoney;
        @BindView(R.id.tv_order_no)
        TextView tvOrderNo;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_delete)
        TextView tvDelete;
        @BindView(R.id.tv_information)
        TextView tvInformation;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnCallBackListener {
        void onCallBack(int type, int position);
    }
}
